﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
#if SILVERLIGHT
using ICSharpCode.SharpZipLib.GZip;
#endif

namespace Salient.Net.Tests
{
    /// <summary>
    /// Helpers to support testing gateway across platforms.
    /// While the response processing is not entirely straight forward
    /// and the gateway would be well served if it abstracted this
    /// away from the user, I feel it is a bleed and introduces 
    /// a dependency( wpsharpziplib) which is not related to the primary focus of the
    /// library. So - these tests can serve as further edification
    /// </summary>
    public static class Utils
    {

        ///<summary>
        /// Extract the data of the response
        ///</summary>
        ///<param name="response"></param>
        ///<returns></returns>
        public static byte[] GetResponseData(this WebResponse response)
        {
            using (Stream stream = response.GetResponseStream())
            {
                return ReadFully(stream);
            }
        }

        private static byte[] ReadFully(this Stream stream)
        {
            var buffer = new byte[32768];
            using (var ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                    {
                        return ms.ToArray();
                    }
                    ms.Write(buffer, 0, read);
                }
            }
        }


        ///<summary>
        ///</summary>
        ///<param name = "response"></param>
        ///<returns></returns>
        public static string GetResponseText(this WebResponse response)
        {
#if SILVERLIGHT
            // silverlight platforms vary in implementations
            // especially if ClientHttp is enabled so we will just
            // be safe and check every request.
            using (Stream responseStream = response.GetResponseStream())
            {
                byte[] data = ReadFully(responseStream);
                string text;
                // check for magic numbers
                if (data.Length > 2 && (data[0] == 31 && data[1] == 139))
                {
                    using (var ms = new MemoryStream(data))
                        using (var gzip = new GZipInputStream(ms))
                            using (var reader = new StreamReader(gzip))
                            {
                                text = reader.ReadToEnd();
                            }
                }
                else
                {
                    text = Encoding.UTF8.GetString(data, 0, data.Length);
                }
                return text;
            }
#else
            // Framework requests are always decompressed by the HttpResponse
            using (Stream stream = response.GetResponseStream())
            {
                return Encoding.UTF8.GetString(ReadFully(stream));
            }
#endif
        }

    }
}